import java.io.*;
import java.util.Scanner;

public class PoemRecovery {
    public static void main(String[] args) {
        // 从控制台读取诗歌
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入诗歌：");
        String poem = scanner.nextLine();

        // 恢复诗歌
        StringBuilder sb = new StringBuilder();
        int len = poem.length();
        for (int i = 0; i < len; i++) {
            char c = poem.charAt(i);
            if (isChinese(c)) {
                sb.append(c);
                // 每7个汉字加入一个标点符号
                if (sb.length() % 7 == 0) {
                    if (sb.length() % 2 == 0) {
                        sb.append("。");
                    } else {
                        sb.append("，");
                    }
                }
            }
        }

        // 将恢复后的诗歌写入D盘某个目录下的某个文本文件
        try {
            File file = new File("D:\\poem.txt");
            FileWriter fw = new FileWriter(file);
            fw.write(sb.toString());
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 统计古诗中某个字或词出现的次数
        if (args.length > 0) {
            String word = args[0];
            int count = 0;
            for (int i = 0; i < len; i++) {
                if (poem.charAt(i) == word.charAt(0)) {
                    boolean found = true;
                    for (int j = 0; j < word.length(); j++) {
                        if (i + j >= len || poem.charAt(i + j) != word.charAt(j)) {
                            found = false;
                            break;
                        }
                    }
                    if (found) {
                        count++;
                    }
                }
            }
            System.out.println(word + "出现的次数：" + count);
        }
    }

    // 判断一个字符是否为汉字
    private static boolean isChinese(char c) {
        return c >= 0x4E00 && c <= 0x9FA5;
    }
}
